import 'package:flutter/material.dart';
import 'package:homepad/iot/basic_types.dart';
import 'package:homepad/iot/light_color.dart';
import 'package:homepad/styles.dart';

extension on FavoriteColor {
  static const _ctColor2700k = Color.fromARGB(255, 243, 174, 101);
  Color get color {
    if (mode == ColorMode.colorTemperature) {
      final percent = (colorTemperature - 2700) / (6500 - 2700);
      return Color.alphaBlend(_ctColor2700k.withOpacity(1 - percent), Colors.white);
    }
    return HSVColor.fromAHSV(1.0, hue.toDouble(), saturation / 100, 1.0).toColor();
  }
}

class LightColorPickerView extends StatefulWidget {
  final bool enable;
  final FavoriteColor color;
  final ValueChanged<FavoriteColor>? onChanged;

  final List<FavoriteColor> favorites;
  const LightColorPickerView({
    super.key,
    required this.enable,
    required this.color,
    required this.favorites,
    this.onChanged,
  });

  @override
  State<StatefulWidget> createState() => _LightColorPickerView();
}

class _LightColorPickerView extends State<LightColorPickerView> {
  final int diameter = 54;
  FavoriteColor _color = const FavoriteColor.colorTemperature(colorTemperature: 2700);

  LightColorPickerView? _lastWidget;
  void _updateColor(FavoriteColor color) {
    if (!widget.enable) {
      return;
    }
    setState(() {
      _color = color;
      _lastWidget = widget;
      widget.onChanged?.call(color);
    });
  }

  bool _isCurrentColor(FavoriteColor color) {
    if (color.mode != _color.mode) {
      return false;
    }
    if (color.mode == ColorMode.colorTemperature) {
      return (color.colorTemperature - _color.colorTemperature).abs() < 50;
    }
    return (color.hue - _color.hue).abs() < 4 && (color.saturation - _color.saturation) < 2;
  }

  @override
  Widget build(BuildContext context) {
    final MyStyles styles = Theme.of(context).extension<MyStyles>()!;
    if (_lastWidget == null || _lastWidget != widget) {
      _color = widget.color;
    }

    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: widget.favorites.map<Widget>((favorite) {
        final isCurrent = _isCurrentColor(favorite);
        return GestureDetector(
          onTap: () => _updateColor(favorite),
          child: CircleAvatar(
            radius: diameter / 2,
            backgroundColor:
            favorite.color.computeLuminance() > 0.7 ? const Color(0xFFE4E4E4) : Colors.white,
            child: CircleAvatar(
              radius: (diameter - (isCurrent ? 9 : 6)) / 2,
              backgroundColor: favorite.color,
              child: isCurrent
                  ? Text("编辑",
                      style: styles.textStyle.copyWith(
                        fontFamily: "PingFangMedium",
                        fontSize: 14,
                        color: favorite.color.computeLuminance() > 0.7 ? Colors.black87 : Colors.white,
                      ))
                  : null,
            ),
          ),
        );
      }).toList(),
    );
  }
}